package cn.hzu580.action;

import java.io.File;
import java.io.InputStream;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.hzu580.entity.User;
import cn.hzu580.service.UserService;
import cn.hzu580.util.InputStreamUtil;
import cn.hzu580.vo.UserVO;


@Controller
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<UserVO> {

	private InputStream inputStream;

	//必须为upload
	private File upload;
	
	private User json;
	
	private List<User> jsonArray;
	
	@Resource(name="user")
	private UserService userService;
	
	private UserVO userVO;


	public String register() throws Exception{
		userVO.setHead(upload);
		String result = userService.register(userVO);
		inputStream = InputStreamUtil.create(result);
		return "stream";
	}


	public String login() throws Exception{
		json = userService.login(userVO);
		return "json";
	}


	public String get() throws Exception{
		System.out.println(ServletActionContext.getRequest().getSession().getAttribute("aaa"));
		json = userService.get(userVO);
		return "json";
	}

	
	public String getRank() throws Exception{
		jsonArray = userService.getRank(userVO);
		return "json_array";
	}


	public void approve() throws Exception{
		userVO.setPaper(upload);
		userService.approve(userVO);
	}


	public String sign() throws Exception{
		String result = userService.sign();
		inputStream = InputStreamUtil.create(result);
		return "stream";
	}


	public String modifyTel() throws Exception{
		String result = userService.modifyTel(userVO);
		inputStream = InputStreamUtil.create(result);
		return "stream";
	}


	public String modifyPassword() throws Exception{
		String result = userService.modifyPassword(userVO);
		inputStream = InputStreamUtil.create(result);
		return "stream";
	}
	
	
	public String ownerValidate() throws Exception{
		String result = userService.ownerValidate(userVO);
		inputStream = InputStreamUtil.create(result);
		return "stream";
	}


	public void modifyName() throws Exception{
		userService.modifyName(userVO);
	}


	public void modifyHead() throws Exception{
		userVO.setHead(upload);
		userService.modifyHead(userVO);
	}
	
	
	public void logout() throws Exception{
		userService.logout();
	}

	public String getValidateMsg() throws Exception{
		String result = userService.validateMsg(userVO);
		inputStream = InputStreamUtil.create(result);
		return "stream";
	}

	public String getValidatePic() throws Exception{
		byte[] result = userService.validatePic();
		inputStream = InputStreamUtil.create(result);
		return "stream";
	}
	
	
	public InputStream getInputStream() {
		return inputStream;
	}



	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}



	public UserService getUserService() {
		return userService;
	}



	public void setUserService(UserService userService) {
		this.userService = userService;
	}



	public UserVO getNotice() {
		return userVO;
	}



	public void setNotice(UserVO notice) {
		this.userVO = notice;
	}



	@Override
	public UserVO getModel() {
		userVO = new UserVO();
		return userVO;
	}


	public User getJson() {
		return json;
	}


	public void setJson(User json) {
		this.json = json;
	}


	public List<User> getJsonArray() {
		return jsonArray;
	}


	public void setJsonArray(List<User> jsonArray) {
		this.jsonArray = jsonArray;
	}


	public UserVO getUserVO() {
		return userVO;
	}


	public void setUserVO(UserVO userVO) {
		this.userVO = userVO;
	}


	public File getUpload() {
		return upload;
	}


	public void setUpload(File upload) {
		this.upload = upload;
	}

	
	
}
